package com.example.clientsideapplication;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Map.Entry;
import java.util.AbstractMap.SimpleEntry;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MerchantLogInActivity extends Activity {
	private final static String MY_LOGIN = "myAppLogIn";
	private SharedPreferences pref;
	private TextView userName;
	private TextView password;
	private Socket socket;
	private ObjectInputStream inputReader;
	private ObjectOutputStream outputWriter;
	private int idMerchant;
	private String serverAddress;
	private int serverSocket;
	private DataPackage incomingmessage;
	private DataPackage outputMessage;
	private Context context;
	private int duration = Toast.LENGTH_SHORT;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.merchant_login);
		
		updatePreferencesData();

		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		Intent intent = getIntent();
		serverAddress = intent.getStringExtra("serverAddress");
		serverSocket = intent.getIntExtra("serverSocket", 21111);
	}

	public void onClickLogIn(View view) {
		try {
			Intent intent = getIntent();
			String serverAddress = intent.getStringExtra("serverAddress");
			int serverSocket = intent.getIntExtra("serverSocket", 21111);

			userName = (TextView) findViewById(R.id.login_username_text_view);
			password = (TextView) findViewById(R.id.login_password_text_view);

			pref = getSharedPreferences(MY_LOGIN, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = pref.edit();
			editor.putString("user", userName.getText().toString());
			editor.commit();
			
			context = getApplicationContext();

			if(!(userName.getText().toString().equals("")) && !(password.getText().toString().equals(""))){
				
				socket = new Socket(serverAddress, serverSocket);
				inputReader = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
				outputWriter = new ObjectOutputStream(socket.getOutputStream());

				Entry<String, String> output = new SimpleEntry<String, String>(
						userName.getText().toString(), password.getText()
								.toString());
				outputMessage = new DataPackage(6, output);
				outputWriter.writeObject(outputMessage);

				incomingmessage = (DataPackage) inputReader.readObject();
				idMerchant = (Integer) incomingmessage.getDataPackage();

				socket.close();

				Intent returnIntent = new Intent();

				if (idMerchant > 0) {
					returnIntent.putExtra("idMerchant", idMerchant);
					returnIntent.putExtra("userMerchant", userName.getText()
							.toString());
					setResult(RESULT_OK, returnIntent);

					pref = getSharedPreferences(MY_LOGIN, Context.MODE_PRIVATE);
					SharedPreferences.Editor editor2 = pref.edit();
					editor2.putInt("idMerchant", idMerchant);
					editor2.commit();

					finish();

				} else {
					Toast toast = Toast.makeText(context, "Campi user e/o password errati.",
							duration);
					toast.show();
				}
			}else{
				Toast toast = Toast.makeText(context, "Campi user e/o password vuoti.",
						duration);
				toast.show();
			}

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void updatePreferencesData() {
		TextView user = (TextView) findViewById(R.id.login_username_text_view);
		SharedPreferences prefs = getSharedPreferences(MY_LOGIN,
				Context.MODE_PRIVATE);
		user.setText(prefs.getString("user", ""));
	}

}
